/// <reference path="../typings/index.d.ts" />

import express = require('express');
import ArticleRoutes = require("./config/routes/Routes");
import bodyParser = require("body-parser");
import logger = require('morgan');
import favicon = require('serve-favicon');
import cookieParser = require('cookie-parser');
import path = require('path');

let port: any = process.env.PORT || 3000;
let env:string = process.env.NODE_ENV || 'developement';

let app = express();

app.set('port', port);

app.use(bodyParser.json());
app.use(cookieParser());
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));

//设置静态资源路径
app.use(favicon(path.join(__dirname, '../static', 'favicon.ico')));
app.use(express.static(path.join(__dirname, '../static')));

// 接口路由
app.use('/api', new ArticleRoutes().routes);

//默认访问地址
let renderIndex = (req: express.Request, res: express.Response) => {
    res.sendFile(path.resolve(__dirname, '../views/client/index.html'));
}
let imgIndex = (req: express.Request, res: express.Response) => {
    res.sendFile(path.resolve(__dirname, '../views/admin/image.html'));
}
app.get('/', renderIndex);
// app.get('/image', imgIndex);
app.get('/admin', imgIndex);


if(env === 'developement'){
    app.use(function(err, req: express.Request, res: express.Response, next: express.NextFunction) {
        res.status(err.status || 500);
        res.json({
            error: err,
            message: err.message
        });
    });
}
// catch 404 and forward to error handler
app.use(function(req: express.Request, res: express.Response, next) {
    let err = new Error("Not Found");
    next(err);
});

// production error handler
// no stacktrace leaked to user
app.use(function(err: any, req: express.Request, res: express.Response, next: express.NextFunction) {
    res.status(err.status || 500);
    res.json({
        error: {},
        message: err.message
    });
});

export = app 